package com.teaRestaurant.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaRestaurant.common.constant.MessageConstant;
import com.teaRestaurant.common.exception.AccountNotFoundException;
import com.teaRestaurant.common.exception.ChangeAdminException;
import com.teaRestaurant.common.exception.PasswordErrorException;
import com.teaRestaurant.domain.DTO.EmployeeLoginDTO;
import com.teaRestaurant.domain.DTO.EmployeePageQueryDTO;
import com.teaRestaurant.domain.VO.EmployeeLoginVO;
import com.teaRestaurant.domain.VO.EmployeePageQueryVO;
import com.teaRestaurant.domain.entity.Employee;
import com.teaRestaurant.mapper.EmployeeMapper;
import com.teaRestaurant.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public EmployeeLoginVO login(EmployeeLoginDTO employeeLoginDTO) {
        String phone = employeeLoginDTO.getPhone();
        String password = employeeLoginDTO.getPassword();
        //1、利用手机号查询数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(phone!=null,Employee::getPhone,phone);
        List<Employee> employeeList = employeeMapper
                .selectList(wrapper);
        //2、如果查询的列表为空，返回异常
        if(employeeList ==null || employeeList.size() < 1) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        // 查看账户是否可用
        if (employeeList.get(0).getStatus() != 1) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_USE);
        }

        //2、密码比对
        //2.1、 对密码进行MD5
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employeeList.get(0).getPassword())) {
            //2.2、密码错误，返回异常
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        // 3、验证成功，返回（userId）
        return EmployeeLoginVO.builder()
                .id(employeeList.get(0).getId())
                .name(employeeList.get(0).getName())
                .build();
    }

    @Override
    public IPage<EmployeePageQueryVO> list(EmployeePageQueryDTO employeePageQueryDTO) {
        int pageNum = employeePageQueryDTO.getPageNum();
        int pageSize = employeePageQueryDTO.getPageSize();
        IPage<Employee> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Employee::getName,employeePageQueryDTO.getName());
        IPage<Employee> employeeIPage = employeeMapper.selectPage(page, wrapper);
        IPage<EmployeePageQueryVO> employeePageQueryVOIPage = new Page<>();
        BeanUtils.copyProperties(employeeIPage,employeePageQueryVOIPage);
        return employeePageQueryVOIPage;
    }

    @Override
    public void insert(Employee employee) {
        // 设置初始状态
        employee.setStatus(1);
        // 设置初始密码
        String s = "123456";
        // 密码加密
        employee.setPassword(DigestUtils.md5DigestAsHex(s.getBytes()));
        employeeMapper.insert(employee);
    }

    @Override
    public void delete(List <Long> ids) {
        for (Long id : ids) {
            // 管理员不能删除
            if (id == 1) {
                throw new ChangeAdminException(MessageConstant.ADMIN_NOT_DEL);
            }
            // 查看账号是否存在
            Employee employee = employeeMapper.selectById(id);
            // 不存在，报错
            if (employee == null) {
                throw new AccountNotFoundException(employee.getName() + MessageConstant.ACCOUNT_NOT_FOUND);
            }
            // 存在，删除
            employeeMapper.deleteById(id);
        }


    }

    @Override
    public void updateStatus(Long id) {
        // 管理员不能修改
        if (id == 1) {
            throw new ChangeAdminException(MessageConstant.ADMIN_NOT_UPSET);
        }
        // 查看账号是否存在
        Employee employee = employeeMapper.selectById(id);
        // 不存在，报错
        if (employee == null) {
            throw new AccountNotFoundException(employee.getName() + MessageConstant.ACCOUNT_NOT_FOUND);
        }
        // 存在，修改状态
        employee.setStatus(employee.getStatus() == 1?2:1);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getId,employee.getId());
        employeeMapper.update(employee,wrapper);
    }
}
